const express = require('express');
const pool = require('../pool.js');
const r = express.Router();
// 1.登录接口
r.get("/v1/login/:uname&:upwd", (req, res) => {
  var $uname = req.params.uname;
  var $upwd = req.params.upwd;
  // res.send($uname + '……' + $upwd);
  var sql = "SELECT * FROM xz_user WHERE uname=? AND upwd=?";
  pool.query(sql, [$uname, $upwd], (err, result) => {
    if (err) throw err;
    console.log(result);
    if (result.length > 0) {
      res.send("1");
    } else {
      res.send("0");
    }
  });
});
// 2.用户列表
r.get("/v1/list", (req, res) => {
  var sql = "SELECT * FROM xz_user";
  pool.query(sql, (err, result) => {
    if (err) throw err;
    res.send(result);
  });
});
// 3.根据uid删除数据
r.delete("/v1/del/:uid", (req, res) => {
  var $uid = req.params.uid;
  var sql = "DELETE FROM xz_user where uid=?";
  pool.query(sql, [$uid], (err, result) => {
    if (err) throw err;
    if (result.affectedRows > 0) {
      res.send("1");
    } else {
      res.send("0");
    }
  });
});
// 4.根据uid查询用户信息
r.get("/v1/search/:uid", (req, res) => {
  var $uid = req.params.uid;
  var sql = "SELECT * FROM xz_user WHERE uid=?";
  pool.query(sql, [$uid], (err, result) => {
    if (err) throw err;
    if (result.length > 0) {
      res.send(result);
    } else {
      res.send("0");
    }
  })
});
// 5.根据uid修改用户信息
r.put("/v1/update", (req, res) => {
  var obj = req.body;
  var sql = "UPDATE xz_user set ? where uid=?";
  pool.query(sql, [obj, obj.uid], (err, result) => {
    if (err) throw err;
    if (result.affectedRows > 0) {
      res.send("1");
    } else {
      res.send("0");
    }
  });
});
module.exports = r;